package zust.javaee.controller;

import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import zust.javaee.dto.AjaxReturn;
import zust.javaee.dto.SessionInfo;
import zust.javaee.dto.SignCorporDto;
import zust.javaee.dto.activity.ActivityDto;
import zust.javaee.dto.activity.ActivityDto2;
import zust.javaee.dto.adminadd.ReqFormAddDto;
import zust.javaee.dto.announcement.AnnouncementDto;
import zust.javaee.dto.announcement.AnnouncementDto2;
import zust.javaee.dto.corporation.CorporIndexNumDto;
import zust.javaee.dto.corporation.CorporationDto;
import zust.javaee.dto.player.PlayerLoginedDto;
import zust.javaee.dto.player.PlayerRegisterDto;
import zust.javaee.entity.Tactivity;
import zust.javaee.entity.Tannouncement;
import zust.javaee.enums.Corporation.CorporationStatusEnum;
import zust.javaee.service.ActivityService;
import zust.javaee.service.AnnouncementService;
import zust.javaee.service.CorporationService;
import zust.javaee.service.PlayerService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liy on 2017/1/4.
 */
@Controller
public class PlayerController {

    @Autowired
    private PlayerService playerService;

    @Autowired
    private CorporationService corporationService;

    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private ActivityService activityService;

    @RequestMapping("/login")
    @ResponseBody
    public AjaxReturn<PlayerLoginedDto> dologin(
            HttpServletRequest request,
            HttpSession session,
            @RequestParam(value = "studentId", required = false) String studentId,
            @RequestParam(value = "password", required = false) String password){
        return playerService.doLogin(studentId, password, session);
//        return new AjaxReturn<>(true, "登陆成功", new PlayerLoginedDto(12, "liyans"));
    }

    @RequestMapping("/register")
    @ResponseBody
    public AjaxReturn<PlayerLoginedDto> doRegister(PlayerRegisterDto playerRegisterDto){
        return playerService.doRegister(playerRegisterDto);
    }

    @RequestMapping("/activate")
    @ResponseBody
    public AjaxReturn<PlayerLoginedDto> activate(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "secret", required = false) String secret){
        return playerService.activate(id, secret);
    }


    @RequestMapping("/{playerId}/corpor/min")
    @ResponseBody
    public AjaxReturn<List<CorporIndexNumDto>> corporMin(
            HttpSession session,
            @PathVariable Integer playerId,
            @RequestParam(value = "number", required = false) Integer number){
        if (number != null && number > 0) {
            return new AjaxReturn<>(true, corporationService.getAllCorporationDto(playerId, number, CorporationStatusEnum.AVAILABLE.getStatus()));
        } else {
            return new AjaxReturn<>(false);
        }
    }

    @RequestMapping("/corpor/{corporId}/ann")
    @ResponseBody
    public AjaxReturn<List<AnnouncementDto>> oneCorporAnn(
            @PathVariable Integer corporId,
            @RequestParam(value = "number", required = false) Integer number) {
        List<Tannouncement> anns = announcementService.getAnnouncementByCorporId(corporId, number);
        if (anns.size() > 0)
            return new AjaxReturn<>(true, announcementService.transToDtos(anns));
        else
            return new AjaxReturn<>(true, "找不到更多");
    }

    @RequestMapping("/corpor/{corporId}/activity")
    @ResponseBody
    public AjaxReturn<List<ActivityDto>> oneCorporActivity(
            @PathVariable Integer corporId,
            @RequestParam(value = "number", required = false) Integer number) {
        List<Tactivity> activities = activityService.getActivityByCorporId(corporId, number);
        if (activities.size() > 0)
            return new AjaxReturn<>(true, activityService.transToDtos(activities));
        else
            return new AjaxReturn<>(false, "数据");
    }

    @RequestMapping("/{playerId}/signcorpor/{corporId}")
    @ResponseBody
    public AjaxReturn playerSignUpCorpor(
            ReqFormAddDto reqFormAddDto,
            @PathVariable Integer playerId,
            @PathVariable Integer corporId){
        if (corporationService.checkOnRequestion(playerId, corporId))
            return new AjaxReturn(false, "您已提交申请，请等待社团审核");
        return playerService.signUpCorpor(playerId, corporId, reqFormAddDto);
    }

    @RequestMapping("/{playerId}/update")
    @ResponseBody
    public AjaxReturn updatePlayer(
            @PathVariable Integer playerId,
            PlayerLoginedDto playerLoginedDto){
        try{
            playerService.upadtePlayer(playerId, playerLoginedDto);
            return new AjaxReturn(true, "成功", playerLoginedDto);
        } catch (Exception e){
            return new AjaxReturn(false, "更新失败");
        }
    }


    @RequestMapping("/{playerId}/corporation")
    @ResponseBody
    public AjaxReturn getOnePlayerCorpor(@PathVariable Integer playerId) {
        return playerService.getOnePlayerCorpor(playerId);
    }

    @RequestMapping("/{playerId}/activity")
    @ResponseBody
    public AjaxReturn getOnePlayerActivity(@PathVariable Integer playerId) {
        return playerService.getOnePlayerActivity(playerId);
    }

    @RequestMapping("/{playerId}/annmin")
    @ResponseBody
    public AjaxReturn getOnePlayerAnnMin(@PathVariable Integer playerId) {
        List<AnnouncementDto2> anns = playerService.getOnePlayerAnn(playerId).getData();
        List<AnnouncementDto2> annm = new ArrayList<>();
        for (int i = 0; i < 4 && i<anns.size(); i++) {
            annm.add(anns.get(i));
        }
        if (annm.size() <= 0)
            return new AjaxReturn(true, "您还没加入社团");
        else
            return new AjaxReturn(true, "加载成功", annm);
    }

    @RequestMapping("/{playerId}/activitymin")
    @ResponseBody
    public AjaxReturn getOnePlayerActivityMin(@PathVariable Integer playerId) {
        List<ActivityDto2> acs = playerService.getOnePlayerActivity(playerId).getData();
        List<ActivityDto2> acm = new ArrayList<>();
        for (int i = 0; i < 4 &&i<acs.size(); i++) {

            acm.add(acs.get(i));
        }
        if (acm.size() <= 0)
            return new AjaxReturn(true, "您还没参加过活动");
        else
            return new AjaxReturn(true, "加载成功", acm);
    }

    @RequestMapping("/{playerId}/corpor/add")
    @ResponseBody
    public AjaxReturn createCorporation(
            @PathVariable Integer playerId,
            SignCorporDto signCorporDto){
        return playerService.createCorporation(playerId, signCorporDto);
    }

    @RequestMapping("/{corporId}/player")
    @ResponseBody
    public AjaxReturn getOneCorporAllPlayer(@PathVariable Integer corporId) {
        return playerService.getOneCorporAllPlayer(corporId);
    }


}
